package salaryServlet;

import com.google.gson.Gson;
import com.mysql.cj.util.StringUtils;
import salaryEntity.emIfoEntity;
import tools.BaseResponse;
import tools.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import static salaryDao.employeeDao.getEmployeeById;
import static salaryDao.employeeDao.updateEmployeeInformation;

@WebServlet(urlPatterns = "/ssc/student/update/")
public class updateEmInfo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("utf-8");

        // 获取提交的json字符串
        String jsonStu = RequestUtil.getRequestBody(req);

        Gson gson = new Gson();
        // 将JSON字符串转化为实体类
        emIfoEntity stuBody = gson.fromJson(jsonStu,emIfoEntity .class);


        emIfoEntity employee = getEmployeeById( stuBody.getEmployeeID());

        if (employee != null && !StringUtils.isNullOrEmpty(stuBody.getEmployeename()))
        {
            employee.setEmployeename(stuBody.getEmployeename());
            employee.setEmployeeID(stuBody.getEmployeeID());
            employee.setPassword(stuBody.getPassword());
            employee.setEmail(stuBody.getEmail());
            employee.setSex(stuBody.getSex());
            employee.setPhone(stuBody.getPhone());
            employee.setBirthDay(stuBody.getBirthDay());
        }

        int rows  = updateEmployeeInformation(employee);

        resp.setContentType("application/json");
        BaseResponse<Integer> baseResponse = new BaseResponse<Integer>();
        if (rows > 0)
        {
            baseResponse.setCode(200);
            baseResponse.setMsg("修改成功");
        }
        else
        {
            baseResponse.setCode(500);
            baseResponse.setMsg("修改失败");
        }

        PrintWriter out = resp.getWriter();
        out.print(gson.toJson(baseResponse));
        out.flush();
    }
    }

